package org.geohunter.activity.main;

import org.geohunter.GeocacheFilter;
import org.geohunter.R;
import org.geohunter.menuactions.MenuActions;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/** Sets the contents to one of two Tabs. Manages onCreate/onResume/onPause. */
public class SwitchableMapTab extends TabBase {
    /** Google Maps tab */
    private final TabBase mGoogleTab;
    /** Open Street Map tab */
    private final TabBase mOsmTab;
    
    private final ViewGroup mViewGroup;
    
    private final SharedPreferences mSharedPreferences;
    
    private static final int NONE = 0;
    public static final int GOOGLE = 1;
    public static final int OSM = 2;

    /** When initialized, value is GOOGLE or OSM */
    private int mCurrentContents = NONE;

    public static SwitchableMapTab create(Context context, 
            GuiState guiState, 
            TabBase googleTab, TabBase osmTab) {
        ViewGroup mapFrame = new FrameLayout(context);
        Drawable icon = context.getResources().getDrawable(R.drawable.ic_menu_mapmode);
        SharedPreferences sharedPreferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
        return new SwitchableMapTab(guiState, null, mapFrame, icon,
                googleTab, osmTab, sharedPreferences);
    }
    
    public SwitchableMapTab(GuiState guiState, 
            MenuActions menuActions,
            ViewGroup contentView, Drawable icon,
            TabBase googleTab, TabBase osmTab,
            SharedPreferences sharedPreferences) {
        super("tab_map", "Map", contentView, icon, menuActions);
        mViewGroup = contentView;
        mGoogleTab = googleTab;
        mOsmTab = osmTab;
        mSharedPreferences = sharedPreferences;
    }

    private TabBase getTab(int tab) {
        if (tab == GOOGLE)
            return mGoogleTab;
        if (tab == OSM)
            return mOsmTab;
        return null;
        
    }
    
    public void showTab(int tab) {
        if (mCurrentContents == tab)
            return;

        TabBase oldTab = getTab(mCurrentContents);
        if (mIsResumed && oldTab != null) {
            oldTab.onPause();
        }
        mCurrentContents = tab;
        TabBase newTab = getTab(tab);
        
        mViewGroup.removeAllViews();
        
        if (!mIsResumed)
            return;  //Contents will be shown when switching to the tab
        
        if (!newTab.mIsLoaded) {
            newTab.onCreate();
            newTab.mIsLoaded = true;
        }
        mViewGroup.addView(newTab.getContentView());
        newTab.onResume();
    }
    
    private boolean mIsResumed = false;

    private int getPreferencesMap() {
        String map = mSharedPreferences.getString("maptype", "google");
        if (map.equals("google"))
            return GOOGLE;
        if (map.equals("osm"))
            return OSM;
        return NONE;
    }
    
    @Override
    public void onResume() {
        if (mIsResumed)
            return;
        
        showTab(getPreferencesMap());
        
        mIsResumed = true;
        
        if (mCurrentContents == NONE)
            return;
        TabBase currentTab = getTab(mCurrentContents);
        if (!currentTab.mIsLoaded) {
            currentTab.onCreate();
            currentTab.mIsLoaded = true;
        }
        if (mViewGroup.getChildCount() == 0) {
            //The contents was previously made active when 
            //the SwitchableTab wasn't visible
            mViewGroup.addView(currentTab.getContentView());
        }
        currentTab.onResume();
    }

    @Override
    public void onPause() {
        if (!mIsResumed)
            return;
        mIsResumed = false;
        TabBase currentTab = getTab(mCurrentContents);
        if (currentTab == null)
            return;
        currentTab.onPause();        
    }

    @Override
    public MenuActions getMenuActions() {
        TabBase currentTab = getTab(mCurrentContents);
        if (currentTab == null)
            return new MenuActions();
        return currentTab.getMenuActions();
    }

    @Override
    public boolean onContextItemSelected(MenuItem menuItem) {
        TabBase currentTab = getTab(mCurrentContents);
        if (currentTab == null)
            return false;
        return currentTab.onContextItemSelected(menuItem);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        TabBase currentTab = getTab(mCurrentContents);
        if (currentTab == null)
            return false;
        return currentTab.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onDataViewChanged(GeocacheFilter filter, boolean isTabActive) {
        mGoogleTab.onDataViewChanged(filter, (mCurrentContents == GOOGLE));
        mOsmTab.onDataViewChanged(filter, (mCurrentContents == OSM));
    }
}
